package sample.qiplat.plugin.flog;


import static com.qiplat.open.ui.util.DensityUtils.dipToPx;

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiplat.open.ui.util.DensityUtils;
import com.rapid.api.component.widget.log.LogGroupView;
import com.rapid.api.component.widget.log.LogView;
import com.rapid.api.ext.plugin.AndroidPlugins;
import com.rapid.api.logging.Diagnostic;
import com.rapid.api.logging.LogCategory;
import com.rapid.api.logging.LogMessage;
import com.rapid.util.android.ContextUtils;

public class FloatLogView extends LinearLayout implements LogGroupView {
    private com.rapid.android.ui.widget.log.LogGroupView logGroupView;
    private TextView titleView;
    private ImageView hideView;
    private OnHideClickListener listener;

    public FloatLogView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        setOrientation(VERTICAL);
        Context pluginCtx = AndroidPlugins.getContext(this);
        this.setBackground(ContextUtils.getDrawable(
                pluginCtx,
                R.drawable.bac_log_panel
        ));
        //顶部栏
        this.titleView = new TextView(context);
        this.hideView = new ImageView(context);
        titleView.setText("日志");
        titleView.setTextSize(16f);
        LinearLayout topBar = new LinearLayout(context);
        topBar.setGravity(Gravity.CENTER);
        addView(topBar, dipToPx(context, 260), dipToPx(context, 36));
        int titleMargin = dipToPx(context, 6);
        LinearLayout.LayoutParams titleParams = new LayoutParams(-2, -1,1);
        titleParams.leftMargin = titleMargin;
        titleParams.topMargin = titleMargin;
        topBar.addView(titleView, titleParams);
        hideView.setImageDrawable(ContextUtils.getDrawable(
                pluginCtx,
                R.drawable.ic_hide
        ));
        int padding = dipToPx(context, 4);
        hideView.setPadding(padding, padding, padding, padding);
        setHideView(hideView);
        int width = dipToPx(context, 36);
        topBar.addView(hideView, width, width);
        //日志视图
        this.logGroupView = new com.rapid.android.ui.widget.log.LogGroupView(context);
        LinearLayout.LayoutParams logParams = new LayoutParams(-1, dipToPx(context, 320) ,1);
        this.addView(logGroupView, logParams);
    }

    private void setHideView(ImageView view) {
        view.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick();
            }
        });
    }

    @Override
    public void sendLog(LogCategory logCategory, LogMessage logMessage) {
        logGroupView.sendLog(logCategory, logMessage);
    }

    @Override
    public void sendLog(LogCategory logCategory, Diagnostic diagnostic) {
        logGroupView.sendLog(logCategory, diagnostic);
    }

    @Override
    public void sendLog(LogCategory logCategory, Throwable throwable) {
        logGroupView.sendLog(logCategory, throwable);
    }

    @Override
    public void setOnHyperLinkClickListener(LogView.OnHyperLinkClickListener onHyperLinkClickListener) {
        logGroupView.setOnHyperLinkClickListener(onHyperLinkClickListener);
    }

    @Override
    public void clear(LogCategory logCategory) {
        logGroupView.clear(logCategory);
    }

    @Override
    public void clearAll() {
        logGroupView.clearAll();
    }

    public void setOnHideClickListener(OnHideClickListener listener) {
        this.listener = listener;
    }

    public interface OnHideClickListener {
        void onClick();
    }
}
